package com.mayn.util;

import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * WebDriver 工具类
 * 
 * @author Administrator
 *
 */
@SuppressWarnings("unused")
public class WebDriverUtil {

	public static void moveToElement(WebDriver driver, By locator) {
		Actions builder = new Actions(driver);
		builder.moveToElement(driver.findElement(locator)).perform();
	}

	public static void moveToElementAndClick(WebDriver driver, By locator) {
		Actions action = new Actions(driver);
		action.moveToElement(driver.findElement(locator)).click().perform();
	}

	public static void waitUntilVisible(WebDriver driver, WebElement element) {
		WebDriverWait waiting = new WebDriverWait(driver, 10);
		//selenium3写法
		waiting.until(new Function<WebDriver, WebElement>() {
			public WebElement apply(WebDriver driver) {
				return element;
			}
		});
		// selenium2写法
		// waiting.until(ExpectedConditions.visibilityOf(element));
	}

	public static void waitUntilClickable(WebDriver driver, By locator) {
		WebDriverWait waiting = new WebDriverWait(driver, 10);
		//selenium3写法
		waiting.until(new Function<WebDriver, WebElement>() {
			public WebElement apply(WebDriver driver) {
				return driver.findElement(locator);
			}
		});
		// selenium2写法
		// waiting.until(ExpectedConditions.elementToBeClickable(locator));
	}

}
